package com.sync.utils.project.demo.pojo;

import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;

/**
 * TODO
 *
 * @Author YWF
 * @Date 2023/6/20 17:54
 */
public class OrderDelayed implements Delayed {

    private String orderNo;

    // 触发时间
    private long time;

    private String taskName;

    public OrderDelayed(String orderNo, long time, TimeUnit unit) {
        this.orderNo = orderNo;
        this.time = System.currentTimeMillis() + ( time>0 ? unit.toMillis(time) : 0);
        taskName = time + "秒任务";
    }

    @Override
    public long getDelay(TimeUnit unit) {
        return unit.convert(this.time-System.currentTimeMillis(),TimeUnit.MILLISECONDS);
    }

    @Override
    public int compareTo(Delayed o) {
        OrderDelayed orderDelayed = (OrderDelayed)o;
        long diff = this.time - orderDelayed.time;
        if (diff<=0){
            return -1;
        }else {
            return 1;
        }
    }

    public String getOrderNo() {
        return orderNo;
    }

    public String getTaskName() {
        return taskName;
    }
}
